package com.unionx.unionxuf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.unionx.unionxuf.common.BillPram;
import com.unionx.unionxuf.common.CredenceDTO;
import com.unionx.unionxuf.common.Page;
import com.unionx.unionxuf.common.Prams;
import com.unionx.unionxuf.common.RMDTD;
import com.unionx.unionxuf.common.Zhuangtai;
import com.unionx.unionxuf.entity.BillMain;
import com.unionx.unionxuf.entity.BillSub;
import com.unionx.unionxuf.entity.BillsubP;
import com.unionx.unionxuf.entity.BillsunPrams;
import com.unionx.unionxuf.entity.Credence;
import com.unionx.unionxuf.entity.CropDTD;
import com.unionx.unionxuf.entity.MXaccount;
import com.unionx.unionxuf.entity.ReceivepayManger;
import com.unionx.unionxuf.entity.Settlestyle;
import com.unionx.unionxuf.entity.SublistDTD;
import com.unionx.unionxuf.localdao.BillMainMapper;
import com.unionx.unionxuf.localdao.BillSubMapper;
import com.unionx.unionxuf.localdao.ComBillMapper;
import com.unionx.unionxuf.localdao.CredenceMapper;
import com.unionx.unionxuf.localdao.ProjectinfoMapper;
import com.unionx.unionxuf.localdao.ReceivepayMangerMapper;
import com.unionx.unionxuf.localdao.SettlestyleMapper;



@Service("billMainService")
public class BillMainServiceImpl implements BillMainService {
	
	@Autowired(required = true)
	private BillMainMapper  billMainMapper;
	
	@Autowired
	private BillSubMapper billSubMapper;
	
	@Autowired 
	private SettlestyleMapper sm;
	
	@Autowired
	private ReceivepayMangerMapper rmm;
	
	@Autowired
	private  CredenceMapper cm;
	@Autowired
	private ProjectinfoMapper pf;
	
	@Autowired
	private ComBillMapper cbm;
	
	
	@Autowired(required = true)
	private DataSourceTransactionManager localtransactionManager;

	DefaultTransactionDefinition def = new DefaultTransactionDefinition();
	
	private Map<String, Object> map = new HashMap<String, Object>();
	
	@SuppressWarnings("unchecked")
	public Object getPros(Integer pageSize, Integer pageNumber) {

	
		Page p=new Page();
		p.setPagesize(pageSize);
		p.setPagenum(pageNumber);
	

			List<BillMain> list= billMainMapper.getAll(p);
			
			int count=billMainMapper.getAllcount();
			map.put("page", pageNumber);
			map.put("total", count + "");		
			map.put("rows", list);


		
		return map;
	
	}

	@Override
	public int saveBillmain(BillMain b) {
		// TODO Auto-generated method stub
		
		return	billMainMapper.insert(b);
		
		 
	}

	@Override
	public Object getBillsubList(String cropid, String serialno) {
		// TODO Auto-generated method stub

		CropDTD c = new CropDTD();
		c.setCorpid(cropid);
		c.setIsno(serialno);
		List<BillSub> list = billSubMapper.getProbyLsandCid(c);
		return list;
	}

	public Object getBillamin(String cropid, String serialno) {
		map.clear();
		try {
			CropDTD cd = new CropDTD();
			cd.setCorpid(cropid);
			cd.setIsno(serialno);
			BillMain bm = billMainMapper.getProbyLsandCid(cd);
			
			BillMain bm1 =  new BillMain();
			bm1.setId(bm.getId());
			bm1.setDate(bm.getDate());
			bm1.setNo(bm.getNo());
			bm1.setSerialno(bm.getSerialno());
			bm1.setInvoicehead(bm.getInvoicehead());
			bm1.setCustomer(bm.getCustomer());
			bm1.setTelno(bm.getTelno());
			bm1.setOtherDepa(bm.getOtherDepa());
			bm1.setInvoiceno(bm.getInvoiceno());
			bm1.setMemcorp(bm.getMemcorp());
			bm1.setMarkingdepa(bm.getMarkingdepa());
			bm1.setCardtime(bm.getCardtime());
			bm1.setDiscount(bm.getDiscount());
			bm1.setConsultationFied(bm.getConsultationFied());
			bm1.setReservatime(bm.getReservatime());
			bm1.setOutboundorder(bm.getOutboundorder());
	/*		if(bm.getOutboundorder().equals("1")){
			bm1.setOutboundorder("有");
			}
			if(bm.getOutboundorder().equals("0")){
			bm1.setOutboundorder("无");	
			}*/
			
			bm1.setConsultationTel(bm.getConsultationTel());
			bm1.setConsultationSite(bm.getConsultationSite());
			bm1.setOutpatientNurse(bm.getOutpatientNurse());
			bm1.setSettlestyle(bm.getSettlestyle());
			bm1.setRemark(bm.getRemark());
			bm1.setSubinfo(bm.getSubinfo());
			bm1.setShihoujine(bm.getShihoujine());
			bm1.setBillmaincol(bm.getBillmaincol());
			bm1.setBillmaincol1(bm.getBillmaincol1());
			bm1.setBillmaincol2(bm.getBillmaincol2());
			bm1.setBillmaincol3(bm.getBillmaincol3());
			bm1.setYajinzhuanru(bm.getYajinzhuanru());
			bm1.setShoushushuoming(bm.getShoushushuoming());
			bm1.setCropid(bm.getCropid());
			bm1.setSfpz(bm.getSfpz());
			bm1.setShenhe(bm.getShenhe());
			bm1.setZuofei(bm.getZuofei());
			bm1.setZhuangtai(bm.getZhuangtai());
			bm1.setShoukuanren(bm.getShoukuanren());
			map.put("status", 0);
			map.put("message", "方法请求成功");
			map.put("data", bm1);
		} catch (Exception e) {
			map.put("status", 1);
			map.put("message", "方法请求失败");
		}
		return map;
	}

public Object getBillaminList(String cropid, String serialno) {
		
	    List<BillMain> list = new ArrayList<BillMain>();
		try {
            CropDTD c = new CropDTD();
			c.setCorpid(cropid);
			c.setIsno(serialno);
			BillMain bm = billMainMapper.getProbyLsandCid(c);
			list.add(bm);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

public Object getStList(String cropid, String serialno) {
	
	   
	
	List<Settlestyle> list = new ArrayList<Settlestyle>();
	try {
		list = sm.getSS(cropid, serialno);
		
	
	} catch (Exception e) {
		e.printStackTrace();
	}
	return list;
}

@Override
public Object getRmList(String cropid, String serialno) {
	// TODO Auto-generated method stub
	 
		
		List<ReceivepayManger> list = new ArrayList<ReceivepayManger>();
		List<RMDTD> list2 = new ArrayList<RMDTD>();
		try {
			list = rmm.selectBylsandCidss(cropid, serialno);
			for(int  i=0;i<list.size();i++){
				RMDTD rd = new RMDTD();
				ReceivepayManger rm = list.get(i);
				rd.setId(rm.getId());
				rd.setType(rm.getType()==1?"应收":"应付");
				rd.setSubjectcode(rm.getSubjectcode());
				rd.setSubjectname(rm.getSubjectname());
				rd.setCorp(rm.getCorp());
				rd.setAmount(rm.getAmount());
				rd.setCorpid(rm.getCorpid());
				rd.setIsno(rm.getIsno());
				rd.setRemark(rm.getRemark());
				rd.setZhuangtai(rm.getZhuangtai());
				list2.add(rd);
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list2;
}

@Override
public Map<String, Object> getP(BillPram b) {
	// TODO Auto-generated method stub
	List<BillMain> list= billMainMapper.getAll2(b);	
	int count=billMainMapper.getAllcount2(b);
	map.put("page", b.getPagenum());
	map.put("total", count + "");		
	map.put("rows", list);

	
	return map;  
}

@Override
public Map<String, Object> getP3(Zhuangtai z) {
	
	float sum1=0;
	float sum2=0;
	float sum3=0;
	float sum4=0;
	float sum5=0;
	
List<BillMain> list= billMainMapper.getAll3(z);
List<SublistDTD> bmlist= new ArrayList<SublistDTD>();

	for(int i=0;i<list.size();i++){
		SublistDTD bm = new SublistDTD();
		BillMain b = list.get(i);
		bm.setDate(b.getDate());
		bm.setNo(b.getNo());
		bm.setSerialno(b.getSerialno());
		bm.setCustomer(b.getCustomer());
		if(b.getZhuangtai().equals("0")){
			bm.setZhuangtai("未生成");
		}
		if(b.getZhuangtai().equals("1")){
			bm.setZhuangtai("已生成");
		}
		
		if(b.getShenhezhuangtai().equals("0")){
			bm.setShenhezhuangtai("未生成");
		}
		if(b.getShenhezhuangtai().equals("1")){
			bm.setShenhezhuangtai("已生成");
		}
		bm.setRemark(b.getRemark());
		
		List listss = this.sm.getSS(b.getCropid(),b.getSerialno());
		   for (int j = 0; j < listss.size(); j++)
		      {
		        if (((Settlestyle)listss.get(j)).getSettlement().equals("现金")) {
		          bm.setCash("现金");
		          sum1+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
		          bm.setCash(sum1+"");
		        }
		        if (((Settlestyle)listss.get(j)).getSettlement().equals("刷卡POS")) {
		         bm.setPosshuaka("刷卡POS");
		          sum2+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
		          bm.setPosshuaka(sum2+"");
		        }
		  
		        if (((Settlestyle)listss.get(j)).getSettlement().equals("电汇")) {
		        	bm.setDianhui("电汇");
		          sum3+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
		          bm.setDianhui(sum3+"");
		        }
		   
		        if (((Settlestyle)listss.get(j)).getSettlement().equals("划卡")) {
		        	bm.setHuaka("划卡");      
		          sum4+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
		          bm.setHuaka(sum4+"");
		        }
		        if (((Settlestyle)listss.get(j)).getSettlement().equals("押金转入")) {
		        	bm.setYajintrans("押金转入");
		        	sum5+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
		        	bm.setYajintrans(sum5+"");
		        }
		        
		       
		      }
		
		   	sum1=0;
	        sum2=0;
	        sum3=0;
	        sum4=0;
	        sum5=0;
		
		bmlist.add(bm);
	}	
	
	

int count=billMainMapper.getAllcount3(z);
	map.put("page", z.getPagenum());
	map.put("total", count + "");		
	map.put("rows", bmlist);

	
	return map;  
}



public Object getProbyLsandCid(HttpServletRequest request,String cropid, String serialno,String zhuangtai) {
	HttpSession session = request.getSession();

	float mcf = 0;
	float mdf = 0;
	map.clear();
	try {
		CropDTD cd = new CropDTD();
		cd.setCorpid(cropid);
		cd.setIsno(serialno);
		List<BillMain> bm = billMainMapper.getProbyLsandCids(cd);
		List<Settlestyle> ss = (List<Settlestyle>) sm.selectBylsandCid(cropid, serialno,zhuangtai);
		List<ReceivepayManger> rm = (List<ReceivepayManger>) rmm.selectBylsandCid(cropid, serialno,zhuangtai);
		
		int totalbsmc=0;
		List<String> bsmclist = billSubMapper.get(cropid, serialno);
		  for (int i = 0; i < bsmclist.size(); i++) {
		    	totalbsmc += Float.parseFloat(bsmclist.get(i));
			}
		
		
		List<ReceivepayManger> rmmc = (List<ReceivepayManger>) rmm.selectBylsandCidandType(cropid, serialno, "0",zhuangtai);
		List<ReceivepayManger> rmmd = (List<ReceivepayManger>) rmm.selectBylsandCidandType(cropid, serialno, "1",zhuangtai);
		//Credence c = cm.selectBylsandCid(cropid, serialno);
		CredenceDTO ct = new CredenceDTO();
		ct.setCdate(bm.get(0).getDate());
		ct.setCnname((String)session.getAttribute("name"));
	/*	ct.setCtype(c.getCredencetype());
		ct.setCno(c.getCredenceno());*/
	    ct.setZdname((String)session.getAttribute("name"));
	    ct.setShname((String)session.getAttribute("name"));
		 ct.setJzname((String)session.getAttribute("name"));
		List<String> km = new ArrayList<String>();
		for (int i = 0; i < ss.size(); i++) {
			km.add(i, ss.get(i).getSubjectname());
		}
		for (int i = 0; i < rm.size(); i++) {
			km.add(i, rm.get(i).getSubjectname());
		}
		ct.setKmname(km.get(0));
		List<String> mc = new ArrayList<String>();
		List<String> md = new ArrayList<String>();
		for (int i = 0; i < rmmc.size(); i++) {
			mc.add(i, rmmc.get(i).getAmount());
		}

		mc.add(totalbsmc+"");
		for (int i = 0; i < mc.size(); i++) {
			mcf += Float.parseFloat(mc.get(i));
		}

		ct.setMcf(mcf + "");
		for (int i = 0; i < rmmd.size(); i++) {
			md.add(i, rmmd.get(i).getAmount());
		}
		for (int i = 0; i < ss.size(); i++) {
			md.add(i, ss.get(i).getAmount());
		}
		for (int i = 0; i < md.size(); i++) {
			mdf += Float.parseFloat(md.get(i));
		}
		ct.setMdf(mdf + "");
		ct.setRemark(bm.get(0).getRemark());
		ct.setCropid(cropid);
		ct.setSerialno(serialno);
		map.put("status", 0);
		map.put("message", "方法请求成功");
		map.put("data", ct);
	} catch (Exception e) {
		map.put("status", 1);
		map.put("message", "方法请求失败");
	}
	return map;
}



public Object getProbyLsandCids(HttpServletRequest request, String cropid, String serialno, String zhuangtai) {

	HttpSession session = request.getSession();
	float mcf = 0;
	float mdf = 0;
	List<CredenceDTO> list = new ArrayList<CredenceDTO>();
	try {

		CropDTD cd = new CropDTD();
		cd.setCorpid(cropid);
		cd.setIsno(serialno);
		cd.setZhuangtai(zhuangtai);
		List<BillMain> bm = billMainMapper.getProbyLsandCids(cd);// 根据流水号和公司id查询主表信息
		List<Settlestyle> ss = (List<Settlestyle>) sm.selectBylsandCid(cropid, serialno, zhuangtai);// 根据流水号和公司id查询结算方式信息
		List<Settlestyle> ssr=new ArrayList<Settlestyle>();
	//	List<ReceivepayManger> rm = (List<ReceivepayManger>) rmm.selectBylsandCid(cropid, serialno, zhuangtai);// 根据流水号和公司id查询应收应付信息
		List<BillsubP> ll = new ArrayList<BillsubP>();
		List<String> bsmc = billSubMapper.get(cropid, serialno);// 查询子表下的价格
		Prams p = new Prams();
		p.setCropid(cropid);
		p.setSerialno(serialno);
		List<BillSub> blist = billSubMapper.getcusinfo(p);
        String date=billMainMapper.selectBMdate(p);
		List<String> km = new ArrayList<String>();
		for (int i = 0; i < blist.size(); i++) {
			BillSub b = blist.get(i);
			BillsubP bpp=new BillsubP();
			bpp.setMcf(blist.get(i).getPriceAfterDiscount());
			bpp.setKemu(pf.selectBybs(b));
			bpp.setRemark(date+"收款单");//tb
			bpp.setKemuno(pf.selectBybss(b));
			ll.add(bpp);
			String subjectname = pf.selectBybs(b);
		    km.add(subjectname);
		
		}
		

		List<ReceivepayManger> rmmc = (List<ReceivepayManger>) rmm.selectBylsandCidandType(cropid, serialno, "0",
				zhuangtai);// 查询应收应付表里面应付信息
		List<ReceivepayManger> rmmd = (List<ReceivepayManger>) rmm.selectBylsandCidandType(cropid, serialno, "1",
				zhuangtai);// 查询应收信息
		// Credence c = cm.selectBylsandCid(cropid, serialno);//查询凭证信息
		// List<String> km = new ArrayList<String>();
		Iterator<Settlestyle> iter = ss.iterator();
		for (int i = 0; i < ss.size(); i++) {
			while(iter.hasNext()){   
				Settlestyle s = iter.next();   
			    if(s.getSettlement().equals("服务项目")){   
			        iter.remove();   
			    } 
			    else{
			    ssr.add(s);
			    }
			} 
		}
			for (int i = 0; i < ssr.size(); i++) {
            BillsubP bpp=new BillsubP();
			bpp.setMdf(ss.get(i).getAmount());
			bpp.setKemu(ss.get(i).getSubjectname());
			bpp.setRemark(date+"收款单");//
			bpp.setKemuno(ss.get(i).getSubjectcode());
			km.add(i, ss.get(i).getSubjectname());// 结算方式的科目名称
			ll.add(i, bpp);
		}
		

		List<String> mc = new ArrayList<String>();
		List<String> md = new ArrayList<String>();
		for (int i = 0; i < rmmc.size(); i++) {
			BillsubP bpp=new BillsubP();
			bpp.setMcf(rmmc.get(i).getAmount());
			bpp.setKemu(rmmc.get(i).getSubjectname());
		    bpp.setRemark(date+"收款单");//tb
			bpp.setKemuno(rmmc.get(i).getSubjectcode());
			ll.add(i,bpp);
			km.add(i, rmmc.get(i).getSubjectname());
		}
		for (int i = 0; i < rmmd.size(); i++) {
			BillsubP bpp=new BillsubP();
			bpp.setMdf(rmmd.get(i).getAmount());
			bpp.setKemu(rmmd.get(i).getSubjectname());
			bpp.setRemark(date+"收款单");//
			bpp.setKemuno(rmmd.get(i).getSubjectcode());
			ll.add(i,bpp);
			km.add(i, rmmd.get(i).getSubjectname());
		}

		for (int i = 0; i < bsmc.size(); i++) {
			mc.add(i, bsmc.get(i));
		}

		for (int i = 0; i < mc.size(); i++) {
			mcf += Float.parseFloat(mc.get(i));
		}

		for (int i = 0; i < rmmd.size(); i++) {
			md.add(i, rmmd.get(i).getAmount());
		}
		
		for (int i = 0; i < ss.size(); i++) {
			md.add(i, ss.get(i).getAmount());
		}
		for (int i = 0; i < md.size(); i++) {
			mdf += Float.parseFloat(md.get(i));
		}
		
		System.out.println(km.size());
		for (int i = 0; i < ll.size(); i++) {
			ll.get(i).setCropid(cropid);
			ll.get(i).setSerialno(serialno);
			ll.get(i).setCtype(zhuangtai);
			ll.get(i).setCdate(bm.get(0).getDate());
		    ll.get(i).setCnname((String) session.getAttribute("name"));
		    ll.get(i).setCtype("记");
			ll.get(i).setCno(serialno);
		
			
			ll.get(i).setZdname((String) session.getAttribute("name"));
			ll.get(i).setShname((String) session.getAttribute("name"));
			ll.get(i).setJzname((String) session.getAttribute("name"));
			ll.get(i).setZhuangtai(zhuangtai);
       
	
			ll.get(i).setRemark(date+"收款单");//tb
			ll.get(i).setCropid(cropid);
			ll.get(i).setSerialno(serialno);
			ll.get(i).setKmname(km.get(i));
            
			
			
		}
			return ll;

	} catch (Exception e) {
		e.printStackTrace();
	}
	return "error";
}




@Override
public Object getP4(HttpServletRequest request,int pageSize, int pageNum, String cropid) {
	// TODO Auto-generated method stub
	HttpSession session = request.getSession();
	String name = (String) session.getAttribute("name");
	CropDTD dt = new CropDTD();
	dt.setCorpid(cropid);

	List<MXaccount> mxlist = new ArrayList<MXaccount>();

	//查字表
	List<BillSub> bslist = billSubMapper.selectaa(dt);
	System.out.println(bslist.size());
	
	List<String> cash = new ArrayList<String>();
	List<String> pos = new ArrayList<String>();
	List<String> zhipiao= new ArrayList<String>();
	List<String> yajin = new ArrayList<String>();
	List<String> qiankuan = new ArrayList<String>();
	List<String> huaka = new ArrayList<String>();
	List<String> dianhui = new ArrayList<String>();
	List<String> yinhang = new ArrayList<String>();
	String amount = sm.selectByName("现金");
	cash.add(amount);
	String amount2 = sm.selectByName("划卡");
	huaka.add(amount2);
	String amount3 = sm.selectByName("押金转入");
	yajin.add(amount3);
	String amount4 = sm.selectByName("pos刷卡");
	pos.add(amount4);
	String amount5 = sm.selectByName("支票");
	zhipiao.add(amount5);		
	String amount6 = sm.selectByName("欠款");
	qiankuan.add(amount6);
	String amount7 = sm.selectByName("电汇");
	dianhui.add(amount7);
	String amount8 = sm.selectByName("银行转账");
	yinhang.add(amount8);
	
	
	//应收 借
	List<String> jie = new ArrayList<String>();
	//应付 贷
	List<String> dai = new ArrayList<String>();
	
	
	for(int  i=0;i<bslist.size();i++){
		
		MXaccount mx =  new MXaccount();
		
		BillSub bs= bslist.get(i);
		mx.setShoushudoc(bs.getOperationDoctor());//手术医师
		mx.setXfdl(bs.getConsumeclass());//消费大类
		mx.setXmfl(bs.getProject());//项目明细
		mx.setXfmx(bs.getSubinfo());//消费明细
		mx.setSubjectname(bs.getSubinfo());//计入科目名称
		mx.setJfje(bs.getPriceAfterDiscount());//缴费金额
		if(Float.parseFloat(bs.getPrice())==Float.parseFloat(bs.getPriceAfterDiscount())){
			mx.setIddazhe("否");
		}else{
			mx.setIddazhe("是");
		}
		mx.setBeizhu(bs.getRemark());
		mx.setChukudan(bs.getSerialno());//出库单	
		
	
		CropDTD dtd = new CropDTD();
		dtd.setCorpid(cropid);
		dtd.setIsno(bs.getSerialno());
		//查询主表信息
		List<BillMain> bmlist = billMainMapper.getProbyLsandCids(dtd);
	
		for(int j=0;j<bmlist.size();j++){
			BillMain b = bmlist.get(j);
			mx.setDate(b.getDate());//日期
			mx.setCusname(b.getCustomer());//顾客姓名
			mx.setXczx(b.getConsultationFied());//现场咨询
			mx.setWebzx(b.getConsultationSite());//网络咨询
			mx.setPhonezx(b.getConsultationTel());//电话咨询
			mx.setScb(b.getMarkingdepa());//市场部
			mx.setMenzhen(b.getOutpatientNurse());//门诊
			mx.setQita(b.getBillmaincol2());//其他	
			mx.setMembername(name);//会员名称
			mx.setYuyuedate(b.getCardtime());//预约金及办卡时间
			mx.setFapiaohao(b.getInvoiceno());//发票号
			mx.setShoukuanren("");//收款人
		}
		
		

	
		
		//查询应收应付信息表
		List<ReceivepayManger> rlist = rmm.selectByDtd(dtd);
	
			if(rlist.size()==bslist.size()){
			ReceivepayManger rp =rlist.get(i);	
			ReceivepayManger r=  new ReceivepayManger();
			r.setType(1);
			r.setCorp(rp.getCorp());
			String amount11 = rmm.selectbbbb(r);//应收金额
			jie.add(amount11);			
			ReceivepayManger rr=  new ReceivepayManger();
			rr.setType(0);
			rr.setCorp(rp.getCorp());
			String amount22 = rmm.selectbbbb(rr);//应付金额
			dai.add(amount22);		
			mx.setOtherpay(dai.get(i));
			mx.setOtherrecive(jie.get(i));
			
			}else{
				ReceivepayManger rp =rlist.get(0);
				ReceivepayManger r=  new ReceivepayManger();
				r.setType(1);
				r.setCorp(rp.getCorp());
				String amount11 = rmm.selectbbbb(r);//应收金额
				jie.add(amount11);
				ReceivepayManger rr=  new ReceivepayManger();
				rr.setType(0);
				rr.setCorp(rp.getCorp());
				String amount22 = rmm.selectbbbb(rr);//应付金额
				dai.add(amount22);
				
				
				mx.setOtherpay(dai.get(0));
				mx.setOtherrecive(jie.get(0));
			}
			//查询解散方式信息表
			mx.setCash(cash.get(0));
			mx.setShuaka(pos.get(0));
			mx.setZhipiao(zhipiao.get(0));
			mx.setShuakabank(yinhang.get(0));
			mx.setQiankuan(qiankuan.get(0));
			mx.setHuaka(huaka.get(0));
			mx.setYajintrans(yajin.get(0));
			mx.setDianhui(dianhui.get(0));
			
		 mxlist.add(mx);
	
	}
	int count = mxlist.size();
	Map<String,Object> map = new HashMap<String,Object>();
	
	
	
	List<MXaccount> mxfenyeshuju = new ArrayList<MXaccount>();
		int curr=0;
		for(int op=(pageNum-1)*pageSize;op<mxlist.size();op++){
			if(curr==pageSize){
				break;
			}else{
				MXaccount mx = mxlist.get(op);
				mxfenyeshuju.add(mx);
				curr++;
			}
		}
		map.put("page",pageNum );
		map.put("total", count + "");		
		map.put("rows", mxfenyeshuju);

		
	return map;

}



/*@Override
public Object getP4(HttpServletRequest request,int pageSize, int pageNum, String cropid) {
	// TODO Auto-generated method stub
	HttpSession session = request.getSession();
	String name = (String) session.getAttribute("name");
	
	List<MXaccount> mxlist = new ArrayList<MXaccount>();
	
	List<BillMain> bmlist =  billMainMapper.selectCrop(cropid);//差主表
	
	for(int  i=0;i<bmlist.size();i++){
		MXaccount mx =  new MXaccount();
		
		BillMain b= bmlist.get(i);
		
		
		mx.setDate(b.getDate());//日期
		mx.setCusname(b.getCustomer());//顾客姓名
		mx.setXczx(b.getConsultationFied());//现场咨询
		mx.setWebzx(b.getConsultationSite());//网络咨询
		mx.setPhonezx(b.getConsultationTel());//电话咨询
		mx.setScb(b.getMarkingdepa());//市场部
		mx.setMenzhen(b.getOutpatientNurse());//门诊
		mx.setQita(b.getBillmaincol2());//其他	
		mx.setMembername(name);//会员名称
		mx.setYuyuedate(b.getCardtime());//预约金及办卡时间
		mx.setFapiaohao(b.getInvoiceno());//发票号
		mx.setShoukuanren("");//收款人
		
		//}mxlist.add(mx);通用
		
		CropDTD dtd = new CropDTD();
		dtd.setCorpid(cropid);
		dtd.setIsno(b.getSerialno());
		//查询子表信息
		List<BillSub> bslist = billSubMapper.getProbyLsandCid(dtd);
	
		for(int j=0;j<bslist.size();j++){
			BillSub bs = bslist.get(j);
			mx.setShoushudoc(bs.getOperationDoctor());//手术医师
			mx.setXfdl(bs.getConsumeclass());//消费大类
			mx.setXmfl(bs.getProject());//项目明细
			mx.setXfmx(bs.getSubinfo());//消费明细
			mx.setSubjectname(bs.getSubinfo());//计入科目名称
			mx.setJfje(bs.getPriceAfterDiscount());//缴费金额
			if(Float.parseFloat(bs.getPrice())==Float.parseFloat(bs.getPriceAfterDiscount())){
				mx.setIddazhe("否");
			}else{
				mx.setIddazhe("是");
			}
			mx.setBeizhu(bs.getRemark());
			mx.setChukudan(bs.getNo());//出库单	
		}
		
		//查询解散方式信息表
		List<Settlestyle> slist = sm.selectByDtd(dtd);
		for(int m=0;m<slist.size();m++){
			
			Settlestyle s = slist.get(m);
			
			String amount = sm.selectByName(s.getSettlement());
			
			mx.setCash(amount);
			mx.setShuaka(amount);
			mx.setZhipiao(amount);
			mx.setShuakabank("");
			mx.setQiankuan(amount);
			mx.setHuaka(amount);
			mx.setYajintrans(amount);
		}
		
		//查询应收应付信息表
		List<ReceivepayManger> rlist = rmm.selectByDtd(dtd);
		for(int y=0;y<rlist.size();y++){
			ReceivepayManger rp =rlist.get(y);
			mx.setOtherpay(rp.getAmount());
			mx.setOtherrecive(rp.getAmount());
			
		}
		mxlist.add(mx);
	
	}
	int count = mxlist.size();
	Map<String,Object> map = new HashMap<String,Object>();
	
	
	
	List<MXaccount> mxfenyeshuju = new ArrayList<MXaccount>();
		int curr=0;
		for(int op=(pageNum-1)*pageSize;op<mxlist.size();op++){
			if(curr==pageSize){
				break;
			}else{
				MXaccount mx = mxlist.get(op);
				mxfenyeshuju.add(mx);
				curr++;
			}
		}
		map.put("page",pageNum );
		map.put("total", count + "");		
		map.put("rows", mxfenyeshuju);

		
	return map;
}*/




public Object updateCtype(HttpServletRequest request, String cropid, String serialno, String zhuangtai) {
	Settlestyle s=new Settlestyle();
	s.setCropid(cropid);
	s.setIsno(serialno);
	s.setZhuangtai(zhuangtai);
	ReceivepayManger rm=new ReceivepayManger();
	rm.setCorpid(cropid);
	rm.setIsno(serialno);
	rm.setZhuangtai("1");
	BillSub bs=new BillSub();
	bs.setCropid(cropid);
	bs.setSerialno(serialno);
	bs.setZhuangtai("1");
	
	
	BillMain bm=new BillMain();
	bm.setSerialno(serialno);
	bm.setCropid(cropid);
	bm.setZhuangtai("1");
	

	sm.updateCtype(s);
	rmm.updateCtype(rm);
	billSubMapper.updateCtype(bs);
	billMainMapper.updateCtype(bm);
	return null;
}

public List<String> saveR(HttpServletRequest request,String cropid, String serialno,String zhuangtai ) {

	HttpSession session = request.getSession();
	float mcf = 0;
	float mdf = 0;
	//List<String> list = new ArrayList<String>();
	try {
		
		CropDTD cd = new CropDTD();
		cd.setCorpid(cropid);
		cd.setIsno(serialno);
		cd.setZhuangtai(zhuangtai);
		BillMain bm = billMainMapper.getProbyLsandCid(cd);//根据流水号和公司id查询主表信息
		List<Settlestyle> ss = (List<Settlestyle>) sm.selectBylsandCid(cropid, serialno,zhuangtai);//根据流水号和公司id查询结算方式信息
		List<ReceivepayManger> rm = (List<ReceivepayManger>) rmm.selectBylsandCid(cropid, serialno,zhuangtai);//根据流水号和公司id查询应收应付信息
     	List<String> bsmc = billSubMapper.get(cropid, serialno);//查询子表下的价格
     	
     	BillsunPrams bcp =new BillsunPrams();
     	bcp.setCorpid(cropid);
     	bcp.setIsno(serialno);
     	bcp.setZhuangtai(zhuangtai);
     	
     	
/*        List<ReceivepayManger> rmmc = (List<ReceivepayManger>) rmm.selectBylsandCidandType(cropid, serialno, "0",zhuangtai);//查询应收应付表里面应付信息
		List<ReceivepayManger> rmmd = (List<ReceivepayManger>) rmm.selectBylsandCidandType(cropid, serialno, "1",zhuangtai);//查询应收信息
*/		Credence c = cm.selectBylsandCid(cropid, serialno);//查询凭证信息
	 	List<String> km = new ArrayList<String>();
		
	 	for (int i = 0; i < ss.size(); i++) {
			km.add(i, ss.get(i).getSubjectname());//结算方式的科目名称
		}
		for (int i = 0; i < rm.size(); i++) {
			km.add(i, rm.get(i).getSubjectname());//应收应付的科目名称
		}
	 	
		Prams  p = new Prams();
		p.setCropid(cropid);
		p.setSerialno(serialno);
		List<BillSub> blist = billSubMapper.getcusinfo(p);
	    for(int i=0;i<blist.size();i++){
			BillSub b = blist.get(i);
			String subjectname = pf.selectBybs(b);	
			km.add(subjectname);
		}
		

	    return km;
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
	
	
}

@Override
public String findzhuangtai(CropDTD c) {
	// TODO Auto-generated method stub
	
	return billMainMapper.findzhuangtai(c);
}

@Override
public int updateOb(BillMain b) {
	// TODO Auto-generated method stub
	return billMainMapper.updateBylsh(b);
}

public Object deleteBill(HttpServletRequest request, CropDTD c) {
	TransactionStatus status = localtransactionManager.getTransaction(def);
	def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
	def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	map.clear();
	
	try {
		String type=billMainMapper.selectBMType(c);
		String shtype=billMainMapper.selectBMshType(c);

		if(type.equals("1")&&shtype.equals("0")){
		 map.put("status", 2);
		 return map;
		}else if(type.equals("0")&&shtype.equals("1")){
			map.put("status", 3);
			return map;
		}else if(type.equals("1")&&shtype.equals("1")){
			 map.put("status", 4);
			 return map;
		}
		else{
			int a= billMainMapper.deletebycidandlsh(c);
		    int b=billSubMapper.deletebycidandlsh(c);
			int d=sm.deletebycidandlsh(c);
			int e= rmm.deletebycidandlsh(c);
			int f=cbm.deletebydtd(c);
			map.put("status", 0);
			localtransactionManager.commit(status);
			return map;
		}
	} catch (Exception e) {
		localtransactionManager.rollback(status);
	}
	
	return map;
  
}

@Override
public String selectBMType(CropDTD c) {
	// TODO Auto-generated method stub
	return billMainMapper.selectBMType(c);
}

@Override
public Object updateCtype2(CropDTD c) {
	// TODO Auto-generated method stub
	map.clear();
	Settlestyle s=new Settlestyle();
	s.setCropid(c.getCorpid());
	s.setIsno(c.getIsno());
	s.setShenhezhuangtai(c.getZhuangtai());
	
	ReceivepayManger rm=new ReceivepayManger();
	rm.setCorpid(c.getCorpid());
	rm.setIsno(c.getIsno());
	rm.setShenhezhuangtai(c.getZhuangtai());
	
	BillSub bs=new BillSub();
	bs.setCropid(c.getCorpid());
	bs.setSerialno(c.getIsno());
	bs.setShenhezhuangtai(c.getZhuangtai());
	
	
	BillMain bm=new BillMain();
	bm.setSerialno(c.getIsno());
	bm.setCropid(c.getCorpid());
	bm.setShenhezhuangtai(c.getZhuangtai());
	

	int i=sm.updateCtype2(s);
	int k=rmm.updateCtype2(rm);
	int l=billSubMapper.updateCtype2(bs);
	int n=billMainMapper.updateCtype2(bm);
	
/*	if(i>0&&k>0&&l>0&&n>0){*/
		map.put("data", 1);
	//}
	return map;
}

@Override
public String selectBMshType(CropDTD c) {
	// TODO Auto-generated method stub
	return billMainMapper.selectBMshType(c);
}



	
	

}
